package com.ssrs.platform.controller.handler;

import com.ssrs.framework.security.annotation.Priv;
import com.ssrs.framework.web.ApiResponses;
import com.ssrs.framework.web.BaseController;
import com.ssrs.platform.model.CommonFileUploadDTO;
import com.ssrs.platform.util.FileUploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;

/**
 * @author ssrs
 */
@RestController
public class CommonHandler extends BaseController {
    /**
     * 公共上传目录
     */
    public static String BASEDIR = "common";

    /**
     * 通用上传接口
     *
     * @param file
     * @return
     */
    @Priv(login = false)
    @ApiIgnore
    @PostMapping(value = "/common/upload", consumes = "multipart/form-data")
    public ApiResponses<CommonFileUploadDTO> commonUpload(@RequestParam("file") MultipartFile file) throws IOException {
        CommonFileUploadDTO result = FileUploadUtil.upload(BASEDIR, file);
        if (!result.isFlag()) {
            return failure(result.getMessage());
        }
        return success(result);
    }
}
